package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.common.R;
import com.itheima.dto.*;
import com.itheima.entity.SysMenu;
import com.itheima.entity.SysRole;
import com.itheima.entity.SysRoleMenu;
import com.itheima.entity.YddTag;
import com.itheima.service.MenuService;
import com.itheima.service.RoleMenuService;
import com.itheima.service.RoleService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.xml.crypto.Data;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/sys_role")
public class RoleController {
    @Autowired
    private RoleService roleService;

    @Autowired
    private MenuService menuService;
    @Autowired
    private RoleMenuService roleMenuService;

    @GetMapping("/allData")
    public com.itheima.vo.R<List<SysRole>> allData(){
        List<SysRole> sysRoleList = roleService.list();
        return com.itheima.vo.R.success(sysRoleList);
    }

    @GetMapping("/list")
    public R<BeanDto<SysRole>> list(String name, int page, int limit){
        //构建分页条件
        Page<SysRole> pageinfo = new Page<>(page,limit);
        //构建条件查询器
        LambdaQueryWrapper<SysRole> lqw = new LambdaQueryWrapper<>();
        lqw.like(StringUtils.isNotEmpty(name),SysRole::getName,name);

        //查询
        roleService.page(pageinfo,lqw);
        //将得到的数据装进dto中
        //当前页
        long current = pageinfo.getCurrent();

        BeanDto<SysRole> tagDto = new BeanDto();
        //存入得到的数据
        tagDto.setData(pageinfo.getRecords());
        //当前页
        tagDto.setCurrentPage(pageinfo.getCurrent());
        //记录的总数
        tagDto.setCount(pageinfo.getTotal());
        //每页大小
        tagDto.setPoageSize((long)limit);
        //总共多少页
        tagDto.setTotalPages(pageinfo.getTotal()/limit + 1);


        return R.success(tagDto);
    }







    @PostMapping("/add")
    public R add(@RequestBody RoleDto roleDto){
        SysRole sysRole = new SysRole();
        sysRole.setName(roleDto.getName());
        String sysroleId = UUID.randomUUID().toString();
        sysRole.setId(sysroleId);
        sysRole.setDescription(roleDto.getDescription());

        sysRole.setDelFlag("0");
        sysRole.setSuperadmin(0);
        ArrayList<Integer> menuids = roleDto.getMenuids();
        roleService.save(sysRole);
        if ( menuids != null){
            for (Integer menuid : menuids) {
                SysRoleMenu sysRoleMenu = new SysRoleMenu();
                sysRoleMenu.setMenuId(menuid.toString());
                sysRoleMenu.setRoleId(sysroleId);
                roleMenuService.save(sysRoleMenu);
            }
        }


        return R.success("");
    }


    @GetMapping("/del")
    public R<String> del(String id){

        //直接根据id删除
        boolean b = roleService.removeById(id);
        LambdaQueryWrapper<SysRoleMenu> lqw = new LambdaQueryWrapper<>();
        lqw.eq(id != null,SysRoleMenu::getRoleId,id);

        boolean remove = roleMenuService.remove(lqw);
        if (b&&remove){
            //删除成功
            return R.success("");
        }
        //删除失败
        return null;

    }

    @GetMapping("/rowInfo")
    public R<MeanDto> rowInfo(String id){


        LambdaQueryWrapper<SysRoleMenu> lqww = new LambdaQueryWrapper<>();
        lqww.eq(id != null,SysRoleMenu::getRoleId,id);
        //获得所有改id的权限列表
        List<SysRoleMenu> list3 = roleMenuService.list(lqww);
        //遍历列表放入一个数组中 里面的 meanid
        ArrayList<String> arrayList = new ArrayList();

        for (SysRoleMenu sysRoleMenu : list3) {
            String menuId = sysRoleMenu.getMenuId();
            arrayList.add(menuId);
        }
        //ArrayList<Integer> basebase = getbaselist(id, "0");

        //得到mean初始 封装给第一层
        MeanDto meanDto = new MeanDto();
        SysRole byId = roleService.getById(id);
        BeanUtils.copyProperties(byId,meanDto);

        LambdaQueryWrapper<SysMenu> lqw = new LambdaQueryWrapper<>();
        lqw.in(SysMenu::getId,arrayList).eq(SysMenu::getPid,0);
        List<SysMenu> list = menuService.list(lqw);

        ArrayList<DataDto> data = new ArrayList<>();
        for (SysMenu sysMenu : list) {

            DataDto dataDto = new DataDto();
            ArrayList<DataNextDto> children = new  ArrayList<>();
            String mark = sysMenu.getMark();
            Integer id2 = sysMenu.getPid();

            //加载第一层
            Integer idd = sysMenu.getId();//
            modef(dataDto,mark,idd);
            // 得到所有归属当前类的pid
            //ArrayList<Integer> basebase2 = getbaselist(id, idd.toString());
            LambdaQueryWrapper<SysMenu> lambdaQueryWrapper1 = new LambdaQueryWrapper<>();
            //这里的判断条件需要判断所属0之后存不存在里面动态获取1 对应的放入列表中
            lambdaQueryWrapper1.in(SysMenu::getId,arrayList).eq(SysMenu::getPid,sysMenu.getId());
            List<SysMenu> list1 = menuService.list(lambdaQueryWrapper1);

            for (SysMenu menu : list1) {
                DataNextDto dataNextDto = new DataNextDto();

                String mark1 = menu.getMark();
                Integer id1 = menu.getId();
                modef(dataNextDto,mark1,id1);

                Integer idfinl = menu.getPid();
                //ArrayList<Integer> getbaselist = getbaselist(id, id1.toString());
                LambdaQueryWrapper<SysMenu> lambdaQueryWrapper2 = new LambdaQueryWrapper<>();
                lambdaQueryWrapper2.in(arrayList.size()!=0,SysMenu::getId,arrayList).eq(SysMenu::getPid,id1);
                List<SysMenu> list2 = menuService.list(lambdaQueryWrapper2);
                dataNextDto.setChildren(list2);
                children.add(dataNextDto);

            }
            dataDto.setChildren(children);
            data.add(dataDto);


        }
        meanDto.setAuthList(data);

        return R.success(meanDto);
    }


/*    public ArrayList<Integer> getbaselist(String id,String id2){
        LambdaQueryWrapper<SysRoleMenu> lqww = new LambdaQueryWrapper<>();
        lqww.eq(id != null,SysRoleMenu::getRoleId,id);
        //获得所有改id的权限列表
        List<SysRoleMenu> list3 = roleMenuService.list(lqww);
        //遍历列表放入一个数组中 里面的 meanid
        ArrayList<String> arrayList = new ArrayList();



        for (SysRoleMenu sysRoleMenu : list3) {
            String menuId = sysRoleMenu.getMenuId();
            arrayList.add(menuId.toString());
        }


        //得到mendid存在list里面的

        //定义基础0
        LambdaQueryWrapper<SysMenu> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SysMenu::getPid,id2);
        List<SysMenu> list4 = menuService.list(lambdaQueryWrapper);//所有0 对象
        //创建数组的到所有0数组对应的id//动态获取 1 128 132 138
        ArrayList<String> base = new ArrayList<>();
        for (SysMenu sysMenu : list4) {
            Integer id1 = sysMenu.getId();
            base.add(id1.toString());
        }
        //遍历权限得到0权限//
        ArrayList<Integer> basebase = new ArrayList<>();
        for (String o : arrayList) {

            if (base.contains(o)){
                basebase.add(Integer.valueOf(o));
            }
        }
        return basebase;
    }*/

    public void modef(Object dataDto, String mark,Integer id){
        LambdaQueryWrapper<SysMenu> lqw = new LambdaQueryWrapper<>();
        lqw.eq(mark != null,SysMenu::getMark,mark).eq(id != null,SysMenu::getId,id);
        SysMenu one = menuService.getOne(lqw);
        BeanUtils.copyProperties(one,dataDto);

    }

    @PostMapping("/update")
    public R update(@RequestBody MeanNextDto meanNextDto){
        //先删除老的数据
        String id = meanNextDto.getId();
        LambdaQueryWrapper<SysRoleMenu> lqw = new LambdaQueryWrapper<>();
        lqw.eq(id != null,SysRoleMenu::getRoleId,id);
        boolean remove = roleMenuService.remove(lqw);
        //根据新数据插入
        List<Integer> menuids = meanNextDto.getMenuids();
        for (Integer menuid : menuids) {
            SysRoleMenu sysRoleMenu = new SysRoleMenu();
            sysRoleMenu.setMenuId(menuid.toString());
            sysRoleMenu.setRoleId(id);
            roleMenuService.save(sysRoleMenu);
        }


        //修改主表数据//先得到原始数据
        SysRole byId = roleService.getById(id);
        BeanUtils.copyProperties(meanNextDto,byId);
        roleService.updateById(byId);
        return R.success("");
    }



}
